package com.echat.serviceapigateway.security;

import com.echat.serviceapigateway.security.constant.Constants;
import com.echat.serviceapigateway.security.model.authorization.TokenGetLimit;
import com.echat.serviceapigateway.security.service.impl.TokenGetLimitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by chenmohan on 2018/09/05
 *
 * @description
 */
@Component
public class FirstDoing implements ApplicationRunner {

    @Autowired
    TokenGetLimitService tokenGetLimitService;

    @Override
    public void run(ApplicationArguments args) {

        List<TokenGetLimit> tokenGetLimits = tokenGetLimitService.listTokenGetLimit();
        LocalDateTime now = LocalDateTime.now();
        tokenGetLimits.stream().forEach(e -> {
            LocalDateTime refreshTime = e.getRefreshTime();
            if (now.getDayOfMonth() - refreshTime.getDayOfMonth() > 0) {
                e.setGetTimes(Constants.TOKEN_GET_LIMIT);
                e.setRefreshTime(now);
                tokenGetLimitService.modifyTokenGetLimit(e);
            }
        });
    }
}
